{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "74aa2dab",
   "metadata": {},
   "source": [
    "# 1.音频录制\n",
    "* 安装下载如下3个模块：pip install 模块名称\n",
    ">1.install SpeechRecognition  \n",
    ">2.Pyaudio  \n",
    ">3.pydub  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a099249",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone()as source:\n",
    "    audio = r.listen(source)\n",
    "    with open(\"1.wav\")as f:\n",
    "        f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "daf96d41",
   "metadata": {},
   "source": [
    "# 2.语音识别\n",
    ">1.百度api:百度的AI账户--（key,seret)  \n",
    ">2.I/O:input：音频文件（pcm/wav/amr）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "95292c6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdasr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2980e90d",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'bdasr' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [3]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m bdasr_result  \u001b[38;5;241m=\u001b[39m \u001b[38;5;28meval\u001b[39m(\u001b[43mbdasr\u001b[49m\u001b[38;5;241m.\u001b[39masr(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m16k.wav\u001b[39m\u001b[38;5;124m'\u001b[39m))[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mresult\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;241m0\u001b[39m]\n\u001b[0;32m      2\u001b[0m bdasr_result\n",
      "\u001b[1;31mNameError\u001b[0m: name 'bdasr' is not defined"
     ]
    }
   ],
   "source": [
    "bdasr_result  = eval(bdasr.asr('16k.wav'))['result'][0]\n",
    "bdasr_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "599984f3",
   "metadata": {},
   "source": [
    "# 3.连接自动回复字典（openai-chatgpt）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39e7edfc",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa = {\n",
    "    \"你是谁？\":\"我是集美貌和智慧于一体的智能小网同学\",\n",
    "    '你好。':\"你也好，今天听起来心情不错哦\",\n",
    "    '北京科技馆。':\"中国科学技术馆，位于北京市朝阳区北辰东路5号，东临亚运居住区，西濒奥运水系，南依奥运主体育场，北望森林公园，于1958年筹建，建设分为两期工程和新馆工程，分别于1988年9月22日、2000年4月29日、2009年9月16日开放。\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff8473a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa_result = qa.get(bdasr_result)\n",
    "qa_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2769fb85",
   "metadata": {},
   "source": [
    "# 4.语音合成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "889281ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdtts\n",
    "bdtts.tts(qa_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9a7e29c",
   "metadata": {},
   "source": [
    "# 5.播放音频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "342c4e27",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71a33a70",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
